import 'package:shared_preferences/shared_preferences.dart';

class PreferencesUtil {
  SharedPreferences _preferences;

  static final PreferencesUtil _instance = PreferencesUtil._internal();
  factory PreferencesUtil() {
    return _instance;
  }

  PreferencesUtil._internal() {
    initPreferences();
  }

  void initPreferences() async {
    if (_preferences == null) {
      _preferences = await SharedPreferences.getInstance();
    }
  }

  static Future<int> getInt(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    int value = prefs.getInt(key);
    return value;
  }

  static void setInt(String key, int value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setInt(key, value);
  }

  static void remove(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.remove(key); //删除指定键
    prefs.clear(); //清空键值对
  }
}
